<template>
  <div class="userManagement-container">
    <vab-query-form>
      <vab-query-form-right-panel :span="24">
        <el-form :inline="true" :model="queryForm" @submit.native.prevent>
          <el-form-item>
            <el-input
              v-model.trim="queryForm.name"
              placeholder="请输入视频名"
              clearable
            />
          </el-form-item>
          <el-form-item>
            <el-button icon="el-icon-search" type="primary" @click="queryData">
              查询
            </el-button>
          </el-form-item>
        </el-form>
      </vab-query-form-right-panel>
    </vab-query-form>

    <el-table
      v-loading="listLoading"
      :data="list"
      :height="height"
      :element-loading-text="elementLoadingText"
    >
      <el-table-column
        show-overflow-tooltip
        prop="name"
        label="视频名称"
      ></el-table-column>
      <el-table-column
        show-overflow-tooltip
        prop="size"
        label="视频大小"
      ></el-table-column>
      <el-table-column
        show-overflow-tooltip
        prop="duration"
        label="视频时长"
      ></el-table-column>
      <el-table-column
        show-overflow-tooltip
        prop="createUser"
        label="上传者"
      ></el-table-column>
      <el-table-column
        show-overflow-tooltip
        prop="createTime"
        label="上传时间"
      ></el-table-column>
      <el-table-column show-overflow-tooltip label="操作" width="200">
        <template #default="{ row }">
          <el-button type="text" @click="showdialog(row.id)">
            选择片段
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      :background="background"
      :current-page="queryForm.pageNo"
      :page-size="queryForm.pageSize"
      :layout="layout"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    ></el-pagination>
    <el-dialog title="选择片段" :visible.sync="mergeDialog" width="35%">
      <el-table
        v-loading="dialogLoading"
        :data="detaillist"
        :element-loading-text="elementLoadingText"
      >
        <el-table-column
          show-overflow-tooltip
          prop="name"
          label="名称"
        ></el-table-column>
        <el-table-column
          show-overflow-tooltip
          label="开始时间"
          prop="startTime"
        ></el-table-column>
        <el-table-column
          show-overflow-tooltip
          label="时长"
          prop="duration"
        ></el-table-column>
        <el-table-column show-overflow-tooltip label="操作" prop="operation">
          <template #default="{ row }">
            <el-button type="text" @click="handleMerge(row.id)">
              音视频合成
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        :current-page="dialogqueryForm.pageNo"
        :page-size="dialogqueryForm.pageSize"
        :layout="layout"
        :total="dialogtotal"
        @size-change="dialogSizeChange"
        @current-change="dialogCurrentChange"
      ></el-pagination>
    </el-dialog>
  </div>
</template>

<script>
  import { getdialoglist, getList } from '@/api/videos'
  import { mergeRecord } from '@/api/record'
  export default {
    name: 'Merge',
    components: {},
    data() {
      return {
        list: [],
        detaillist: [],
        mergeDialog: false,
        listLoading: true,
        dialogLoading: false,
        background: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        dialogtotal: 0,
        selectRows: '',
        elementLoadingText: '正在加载...',
        queryForm: {
          name: '',
          pageNo: 1,
          pageSize: 10,
        },
        dialogqueryForm: {
          id: 0,
          pageNo: 1,
          pageSize: 10,
        },
      }
    },
    computed: {
      height() {
        return this.$baseTableHeight()
      },
    },
    created() {
      this.fetchData()
    },
    methods: {
      async showdialog(val) {
        this.dialogqueryForm.id = val
        this.mergeDialog = true
        this.dialogLoading = true
        const result = await getdialoglist(this.dialogqueryForm, val)
        const query = result.data
        console.log(query)
        this.detaillist = query.data
        this.dialogtotal = query.totalResults
        setTimeout(() => {
          this.dialogLoading = false
        }, 500)
      },
      async handleMerge(val) {
        const recordId = this.$route.query.id
        let paykey = {
          clipId: 0,
          recordId: 0,
        }
        paykey.clipId = val
        paykey.recordId = Number(recordId)
        this.mergeDialog = false
        const result = await mergeRecord(paykey)
        let data = result.data
        if (data.success)
          setTimeout(() => {
            this.$baseMessage(`合并完成!`, 'success')
          }, 1000)
        else
          setTimeout(() => {
            this.$baseMessage(data.msg, 'error')
          }, 1000)
      },
      test(val) {},
      handleSizeChange(val) {
        this.queryForm.pageSize = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.queryForm.pageNo = val
        this.fetchData()
      },
      dialogSizeChange(val) {
        this.dialogqueryForm.pageSize = val
        this.showdialog(this.dialogqueryForm.id)
      },
      dialogCurrentChange(val) {
        this.dialogqueryForm.pageNo = val
        this.showdialog(this.dialogqueryForm.id)
      },
      queryData() {
        this.fetchData()
      },
      async fetchData() {
        this.listLoading = true
        const query = await getList(this.queryForm)
        let data = query.data
        let temp = data.data
        for (let i = 0; i < temp.length; i++) {
          temp[i].size = (Number(temp[i].size) / 1024 / 1024).toFixed(2)
        }
        this.list = temp
        this.total = data.totalResults
        setTimeout(() => {
          this.listLoading = false
        }, 300)
      },
    },
  }
</script>
